package com.itbaizhan.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
import java.util.Set;

public class SensitiveWordInterceptor implements HandlerInterceptor {
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        String[] sensitiveWords = {"sb","白痴"};
        Map<String, Object> model = modelAndView.getModel();
        Set<Map.Entry<String, Object>> entries = model.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            String key = entry.getKey();
            String val = entry.getValue().toString();
            for (int i=0; i<sensitiveWords.length; i++){
                if (val.contains(sensitiveWords[i])){
                    val = val.replace(sensitiveWords[i], "***");

                }
                model.put(key,val);
            }
        }
    }
}
